<template>
  <div style="margin: 20px;">
    <h1 style="text-align: center">发布新课程</h1>
    <el-steps :active="0" process-status="finish" align-center>
      <el-step title="填写课程基本信息"></el-step>
      <el-step title="创建课程大纲"></el-step>
      <el-step title="最终发布"></el-step>
    </el-steps>

    <el-form :model="course" label-width="100px" class="demo-ruleForm">
      <el-form-item label="课程标题">
        <el-input v-model="course.title"/>
      </el-form-item>
      <el-form-item label="课程分类">
        <el-select v-model="course.subjectParentId" @change="subjectChange1" placeholder="一级分类">
          <el-option
            v-for="item in subjectList1"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
        <el-select v-model="course.subjectId" placeholder="二级分类">
          <el-option
            v-for="item in subjectList2"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="课程讲师">
        <el-select v-model="course.teacherId" placeholder="请选择">
          <el-option
            v-for="item in teacherList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="总课时">
        <el-input-number v-model="course.lessonNum" controls-position="right" :min="0"/>
      </el-form-item>
      <el-form-item label="课程简介">
        <quill-editor
          v-model="course.description"
        />
      </el-form-item>
      <el-form-item label="课程封面">
        <el-upload
          class="avatar-uploader"
          :action="api+'/upload/cover'"
          :headers="headers"
          :show-file-list="false"
          :on-success="uploadSuccess"
          :on-error="uploadError"
        >
          <img v-if="course.cover" :src="course.cover">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="课程价格">
        <el-input-number v-model="course.price" controls-position="right" :min="0"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存并下一步</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {teaList} from '@/api/teacher'
import {subGetTree} from '@/api/subject'
import {couGetById, couSaveOrUpdate} from '@/api/course'
import {getToken} from '@/utils/auth'

export default {
  data() {
    return {
      course: {
        teacherId: '',
        subjectId: '',
        subjectParentId: '',
        cover: '',
        lessonNum: 0,
        price: 0
      },
      api: process.env.VUE_APP_BASE_API,
      headers: { token: getToken() },
      teacherList: [],
      subjectList1: [],
      subjectList2: []
    }
  },
  created() {
    teaList().then(res => {
      this.teacherList = res.data
    })
    subGetTree().then(res => {
      this.subjectList1 = res.data
      if (this.$route.query.courseId) {
        couGetById(this.$route.query.courseId).then(res => {
          this.course = res.data
          this.subjectList1.forEach(sub => {
            if (this.course.subjectParentId === sub.id) {
              this.subjectList2 = sub.subjects
            }
          })
        })
      }
    })
  },
  methods: {
    subjectChange1() {
      this.course.subjectId = ''
      this.subjectList1.forEach(sub => {
        if (this.course.subjectParentId === sub.id) {
          this.subjectList2 = sub.subjects
        }
      })
    },
    uploadSuccess(response) {
      this.course.cover = response.data
      this.$message.success('上传成功')
    },
    uploadError() {
      this.$message.error('上传失败')
    },
    submitForm() {
      couSaveOrUpdate(this.course).then(res => {
        if (res.code === 200) {
          this.$router.push({ path: '/course/chapter', query: { courseId:this.course.id} })
        } else {
          this.$message.error('保存失败')
        }
      })
    }
  }
}
</script>

<style>
.ql-editor {
  height: 400px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

</style>
